package com.maezia.tdr.dao.repository;

import com.maezia.tdr.common.CommonConstant;
import com.maezia.tdr.common.redis.BaseRedisRepository;
import com.maezia.tdr.dao.mapper.DictMapper;
import com.maezia.tdr.domain.po.Dict;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.util.ObjectUtils;

@Slf4j
@Repository
public class DictRepository extends BaseRedisRepository<Dict> {

    private static String DICT_INFO_KEY = "DICT" + CommonConstant.COLON;

    @Autowired
    private DictMapper dictMapper;

    public DictRepository() {
        super(Dict.class);
    }

    public void save(Dict dict){
        String redisKey = DICT_INFO_KEY + dict.getName();
        redisValueOpsTemporary.set(redisKey, dict);
    }

    public Dict getByKey(String key){
        String redisKey = DICT_INFO_KEY + key;
        Dict dict = redisValueOpsTemporary.get(redisKey);
        if(ObjectUtils.isEmpty(dict)){
            dict = dictMapper.getByKey(key);
            if(!ObjectUtils.isEmpty(dict)){
                save(dict);
            }
        }
        return dict;
    }
}
